package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.itheima.health.mapper.OrderSettingMapper;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import com.itheima.health.utils.date.DateUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @program: health_paret
 * @description:
 * @author: Mr.Wang
 * @create: 2020-11-09 13:15
 **/
@Service
@Transactional
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public void batchOrdeiSetting(List<String[]> strings) {
        List<OrderSetting> orderSettingImportList = changeArraysToList(strings);
        for (OrderSetting orderSetting : orderSettingImportList) {
            QueryWrapper<OrderSetting> orderSettingQueryWrapper = new QueryWrapper<>();
            orderSettingQueryWrapper.eq("ORDERDATE", orderSetting.getOrderDate());
            Integer id = baseMapper.selectOne(orderSettingQueryWrapper).getId();
            orderSetting.setId(id);
        }
        saveOrUpdateBatch(orderSettingImportList);
    }

    //显示前端页面日历下面的预约数以及可预约数
    @Override
    public Map findSettingDate(int year, int month) {
        String beginDate = year + "-" + month + "-1";
        String endDate = year + "-" + month + "-31";
        List<OrderSetting> currentMonthMonth = baseMapper.selectCurrentMonth(beginDate, endDate);
        HashMap<String, Object> map = new HashMap();
        for (OrderSetting orderSetting : currentMonthMonth) {
            Date orderDate = orderSetting.getOrderDate();
            String dateString = DateUtils.parseDate2String(orderDate, "yyyy-MM-dd");
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("number", orderSetting.getNumber());
            orderSettingMap.put("reservations", orderSetting.getReservations());
            map.put(dateString, orderSettingMap);
        }
        return map;
    }
//    进行修改每天最大的预约数
    @Override
    public Boolean updateNumberInfoOrderSetting(int number, String settingDay) {
        Boolean updateNumber = baseMapper.updateNumberInfoOrderSetting(number, settingDay);
        return updateNumber;
    }
//查看当前日期是否可以预约
    @Override
    public int findNumberThenReservations(String date) {
        return baseMapper.findNumberThenReservations(date);
    }
    @LcnTransaction
    @Override
    public void updateReservationByOrderDate(String orderDate) {
        baseMapper.updateReservationByOrderDate(orderDate);
    }


    //这个方法是将获取文件信息的List<String[]> strings  中的String[]  被pojo对象OrderSetting来替换的
    private List<OrderSetting> changeArraysToList(List<String[]> strings) {
        List<OrderSetting> orderSettings = new ArrayList<>();
        if (strings != null && strings.size() > 0) {
            for (String[] string : strings) {
                OrderSetting ordersettingpojo = new OrderSetting();
                ordersettingpojo.setReservations(0);
                ordersettingpojo.setOrderDate(DateUtils.parseString2Date(string[0], "yyyy/MM/dd"));
                ordersettingpojo.setNumber(Integer.parseInt(string[1]));
                orderSettings.add(ordersettingpojo);
            }
        }
        return orderSettings;
    }
}
